﻿Public Class ClassUser
    Public Shared Function AddUser(ByVal emp As Integer, ByVal group As Integer, ByVal name As String, ByVal pwd As String, ByVal logon As String)
        Dim sql = "insert into tbl_users(employee_id,group_id,username,password,change_lockon,active) value(@emp,@group,@name,@pwd,@logon,1)"
        Try
            Cmd.Parameters("@emp") = emp
            Cmd.Parameters("@group") = group
            Cmd.Parameters("@name") = name
            Cmd.Parameters("@pwd") = pwd
            Cmd.Parameters("@logon") = logon
            Cmd.ExecuteNonQuery(sql)
            myMsgbox.show("Add new User sucess!!!", "New User")
            Return True
        Catch ex As Exception
            myMsgbox.show("Add new User fail!!", "New User")
            Return False
        End Try
    End Function

    Public Shared Function editUser(ByVal id As Integer, ByVal name As String, ByVal pwd As String, ByVal group As Integer)
        Dim sql = "update tbl_users set username=@name,password=@pwd,group_id=@group where user_id=@id"
        Try
            Cmd.Parameters("@id") = id
            Cmd.Parameters("@name") = name
            Cmd.Parameters("@pwd") = pwd
            Cmd.Parameters("@group") = group
            Cmd.ExecuteNonQuery(sql)
            myMsgbox.show("Update User Success!!!", "Update User")
            Return True
        Catch ex As Exception
            myMsgbox.show("Update User Fail!!!", "Update User")
            Return False
        End Try
    End Function

    Public Shadows Function deleteUser(ByVal id As Integer)
        Dim sql = "update tbl_users set active=false where user_id=@id"
        Try
            Cmd.Parameters("@id") = id
            Cmd.ExecuteNonQuery(sql)

            myMsgbox.show("Delete User Success!!!", "Delete User")
            Return True
        Catch ex As Exception
            myMsgbox.show("Delete User Fail!!!", "Delete User")
            Return False
        End Try
    End Function

    Public Shared Function loadUser(ByVal text As String) As DataTable
        Dim sql = "select s.*,e.employee_name,g.group_name from tbl_users s inner join tbl_employee e on e.employee_id=s.employee_id inner join tbl_group g on g.group_id=s.group_id where s.active=true and UPPER(username) like UPPER(@name)"
        Try
            Cmd.Parameters("@name") = "%" & text & "%"
            Dim dt As New DataTable
            dt = Cmd.ExecuteDataTable(sql)
            Return dt
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
End Class
